using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class controls_usrYear : System.Web.UI.UserControl
{
    protected Int32 intYear = -1;
    protected Int32 intStartYear = CE.Global.General.GetUTCDateTime().Year - 50;
    protected Int32 intEndYear = CE.Global.General.GetUTCDateTime().Year + 2;
    protected string strValidationGroup = "";
    protected bool bEnabled = true;
    protected string strErrorMessage = "Choose year.";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlYear.Attributes.Add("onblur", "ShowValidationEffect('" + ddlYear.ClientID + "','" + rfvYear.ClientID + "');");
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvYear.Enabled && rfvYear.ValidationGroup == strValidationGroup)
        {
            rfvYear.Validate();
            if (rfvYear.IsValid == false)
            {
                ddlYear.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlYear.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlYear.ControlStyle.CssClass = "";
        }
    }

    //protected void ServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    // Test whether the value is -1
    //    if (args.Value != "-1")
    //    {
    //        args.IsValid = true;
    //    }
    //    else
    //    {
    //        args.IsValid = false;
    //    }
    //}

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public void IsEnabledValidation(bool bValue)
    {
        rfvYear.Enabled = bValue;
    }

    public Int32 Year
    {
        get { return intYear; }
        set { intYear = value; }
    }

    public Int32 StartYear
    {
        get { return intStartYear; }
        set { intStartYear = value; }
    }

    public Int32 EndYear
    {
        get { return intEndYear; }
        set { intEndYear = value; }
    }

    public string ValidationGroup
    {
        get { return strValidationGroup; }
        set { strValidationGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    protected override void OnPreRender(EventArgs e)
    {
        SetEnabled();

        ddlYear.Items.Clear();
        for (int i = intEndYear; i >= intStartYear; i--)
        {
            ddlYear.Items.Add(i.ToString());
        }
        ddlYear.Items.Insert(0, new ListItem("YYYY", "-1"));

        if (intYear > 0)
        {
            ddlYear.Text = intYear.ToString();
        }
        //else
        //{
        //    ddlYear.Text = CE.Global.General.GetUTCDateTime().Year.ToString();
        //}

        if (strValidationGroup != string.Empty)
            rfvYear.ValidationGroup = strValidationGroup;

        if (strErrorMessage != string.Empty)
        {
            rfvYear.ErrorMessage = strErrorMessage;
        }
    }

    private void SetEnabled()
    {
        //ddlYear.SelectedIndex = 0;

        ddlYear.Enabled = bEnabled;

        rfvYear.Enabled = bEnabled;
    }

    public int GetYear()
    {
        if (bEnabled)
        {
            if (rfvYear.IsValid)
            {
                intYear = Convert.ToInt32(ddlYear.Text);
            }
        }

        return intYear;
    }
}
